package edu.xhu.adapters;

import java.util.ArrayList;

import com.bean.AlreadyBookBean;
import com.constant.Constant;
import com.data.RenewBookData;
import com.net.ClientError;
import com.net.LoadData;
import com.net.RequestListener;
import com.net.RequestParameter;

import edu.xhu.R;
import edu.xhu.adapters.LibraryAdapter.ViewHolder;
import edu.xhu.view.LoadingDialog;

import android.app.AliasActivity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class MyLibraryAdapter extends BaseAdapter{

	private ArrayList<AlreadyBookBean> mAlreadyBook;
	private LayoutInflater inflater;
	private Context mContext;
	private String mCookie;
	
	public MyLibraryAdapter(Context context, String cookie, ArrayList<AlreadyBookBean> mAlreadyBook){
		inflater = LayoutInflater.from(context);
		this.mAlreadyBook = mAlreadyBook;
		mContext = context;
		this.mCookie = cookie;
	}
	
	public int getCount() {
		if (null == mAlreadyBook)
			return 0;
		return mAlreadyBook.size();
	}

	public Object getItem(int position) {
		return null;
	}

	public long getItemId(int position) {
		return 0;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		if (convertView == null){
			viewHolder		= new ViewHolder();
			convertView		= inflater.inflate(R.layout.my_library_item, null);
			viewHolder.name = (TextView) convertView.findViewById(R.id.textview_bookname);
			viewHolder.place = (TextView) convertView.findViewById(R.id.textview_place);
			viewHolder.author = (TextView) convertView.findViewById(R.id.textview_author);
			viewHolder.barcode = (TextView) convertView.findViewById(R.id.textview_barcode);
			viewHolder.renewTimes = (TextView) convertView.findViewById(R.id.textview_renew_times);
			viewHolder.borrowDate = (TextView) convertView.findViewById(R.id.textview_borrow_date);
			viewHolder.returnDate = (TextView) convertView.findViewById(R.id.textview_return_date);
			viewHolder.attachment = (TextView) convertView.findViewById(R.id.textview_attachment);
			viewHolder.renew	=	(TextView) convertView.findViewById(R.id.textview_renew);
			
			convertView.setTag(viewHolder);
		}else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		
		final AlreadyBookBean alreadyBookBean = mAlreadyBook.get(position);
		viewHolder.name.setText(alreadyBookBean.title);
		viewHolder.place.setText("馆藏地：" + alreadyBookBean.collectSite);
		viewHolder.author.setText("作者：" + alreadyBookBean.author);
		viewHolder.barcode.setText("条码号：" + alreadyBookBean.bar_code);
		viewHolder.renewTimes.setText("续借次数：" + alreadyBookBean.renewCount);
		viewHolder.borrowDate.setText("借阅日期：" + alreadyBookBean.borrowDate);
		viewHolder.returnDate.setText("应还日期：" + alreadyBookBean.shouldDate);
		viewHolder.attachment.setText("附件：" + alreadyBookBean.attachment);
		
		if (getCount() == 1){
			convertView.setBackgroundResource(R.drawable.full_selector);
		}else {
			if (0 == position){
				convertView.setBackgroundResource(R.drawable.top_selector);
			}else if (getCount()-1 == position){
				convertView.setBackgroundResource(R.drawable.bottom_selector);
			}else {
				convertView.setBackgroundResource(R.drawable.middle_selector);
			}
		}
		
		// 续借图书
		viewHolder.renew.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				LoadingDialog.showWindow(mContext);
				RequestParameter parameter = new RequestParameter();
				parameter.add("bar_code", alreadyBookBean.bar_code);
				parameter.add("check", alreadyBookBean.check);
				parameter.add("cookie", mCookie);
				LoadData.loadData(Constant.RENEW_BOOK_DATA, parameter, new RequestListener() {
					
					@Override
					public void onError(ClientError ed) {
						LoadingDialog.dismiss();
						Toast.makeText(mContext, "续借图书失败", 0).show();
					}
					
					@Override
					public void onComplete(Object obj) {
						LoadingDialog.dismiss();
						RenewBookData data = (RenewBookData) obj;
						if(data != null && data.getStatus() == true) {
							Toast.makeText(mContext, "续借图书成功", 0).show();
						} else  {
							Toast.makeText(mContext, "不到续借时间，不得续借！", 0).show();
						}
						
						
					}
				});
				
			}
		});
		
		return convertView;
	}

	class ViewHolder{
		public TextView name;					//书名
		public TextView place;					//图书管名字
		public TextView author;					//作者
		public TextView barcode;				// 条码号
		public TextView renewTimes;				//续借次数
		public TextView borrowDate;				//借阅日期
		public TextView returnDate;				//归还日期
		public TextView attachment;				//附件
		
		public TextView renew;					// 续借
	}
}
